<template>
  <div>
    <el-container style="height: 100%; border: 1px solid #eee">
      <el-container>
        <el-header style="text-align: right; font-size: 12px">
          <el-button
              type="primary"
              size="small"
              style="margin-left: 1040px "
              @click="batchApproved">批量通过</el-button>
        </el-header>
        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%"
              stripe
              border
              @selection-change="handleSelectionChange"
              max-height="575">
            <el-table-column
                type="selection"
                width="50">
            </el-table-column>
            <el-table-column
                fixed
                prop="createdTime"
                label="申请时间"
                :formatter="formatDate">
            </el-table-column>
            <el-table-column
                prop="studentName"
                label="姓名">
            </el-table-column>
            <el-table-column
                prop="areaName"
                label="园区">
            </el-table-column>
            <el-table-column
                prop="inRoomBuildingName"
                label="转入楼栋">
            </el-table-column>
            <el-table-column
                prop="inRoomName"
                label="转入房间">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作">
              <template slot-scope="scope">
                <el-button type="text" size="small" style="margin-left: 0px" @click="editInformation(scope.row)">编辑</el-button>
                <el-button type="text" size="small" style="margin-left: 20px" @click="approveApplication(scope.row)">通过</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer>
          <div class="block">
            <el-pagination
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total=totalNum>
            </el-pagination>
          </div>
        </el-footer>
      </el-container>
    </el-container>
    <el-dialog title="入住审核" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="申请时间:" :label-width="formLabelWidth" >
          {{this.form.time}}
        </el-form-item>
        <el-form-item label="姓名:" :label-width="formLabelWidth">
          {{this.form.name}}
        </el-form-item>
        <el-form-item label="转入楼栋" :label-width="formLabelWidth">
          <el-select v-model="form.building" placeholder="请选择楼栋">
            <el-option
                v-for="item in buildings"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getAllAdjustApplication, getBuildingsByGender, updateAdjustByLeader, approveAdjustByLeader} from '../../../../../api/leader'

export default {
  data () {
    return {
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {
        ID: null,
        name: null,
        time: null,
        area: null,
        building: null
      },
      pageSize: 5,
      totalNum: 0,
      pageNo: 1,
      DOT: {
        pageSize: 5,
        pageNo: 1,
        data: {
          ID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
      },
      buildings: [],
      formLabelWidth: '120px'
    }
  },

  methods: {
    formatDate (row) {
      const date = new Date(row.createdTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    editInformation (row) {
      this.form.name = row.studentName
      this.form.time = this.formatDate(row)
      this.form.area = row.areaName
      this.dialogFormVisible = true
      let obj = {
        ID: row.id.toString()
      }
      this.form.ID = obj.ID
      getBuildingsByGender(obj).then(res => {
        this.buildings = res.data
      })
    },
    sendForm () {
      console.log(this.form)
      if (this.form.building === null) {
        this.$message({
          message: '请选择楼栋',
          type: 'error'
        })
      } else {
        let obj = {
          ID: this.form.ID,
          buildingID: this.form.building.toString()
        }
        updateAdjustByLeader(obj).then(res => {
          if (res.status === true) {
            this.queryAllAdjustApplication()
          } else {
            this.$message({
              message: '失败',
              type: 'error'
            })
          }
        })
        this.dialogFormVisible = false
      }
    },
    approveApplication (row) {
      if (row.inRoomBuildingName === null) {
        this.$message({
          message: '请选择转入楼栋',
          type: 'error'
        })
      } else {
        let obj = {
          ID: row.id.toString(),
          state: '分配中',
          leaderID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
        if (row.inRoomName !== null) {
          obj.state = '已完成'
        }
        approveAdjustByLeader(obj).then(res => {
          if (res.status === true) {
            this.queryAllAdjustApplication()
          } else {
            this.$message({
              message: '失败',
              type: 'error'
            })
          }
        })
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    queryAllAdjustApplication () {
      getAllAdjustApplication(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    handleCurrentChange (newPage) {
      this.pageNo = newPage
      this.DOT.pageNo = newPage
      this.queryAllAdjustApplication()
    },
    batchApproved () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请至少选择一项进行批量审批',
          type: 'warning'
        })
        return
      }
      Promise.all(this.multipleSelection.map(row => {
        if (row.inRoomBuildingName === null) {
          this.$message({
            message: '请选择转入楼栋',
            type: 'warning'
          })
        }
        let approvalState = row.inRoomName !== null ? '已完成' : '分配中'
        let obj = {
          ID: row.id.toString(),
          state: approvalState,
          leaderID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
        if (row.inRoomBuildingName !== null) {
          approveAdjustByLeader(obj).then(res => {
            if (!res.status) {
              throw new Error('审批失败')
            } else {
              this.queryAllAdjustApplication()
            }
          })
        }
      }))
    }

  },
  created () {
    this.queryAllAdjustApplication()
  }
}
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>
